﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Actuals.Business;
using Actuals.CoreDomain.Business;
using Actuals.CoreDomain.Data;
using Actuals.CoreDomain;
using Actuals.Common;


namespace Actuals
{
    public partial class ShowUserStory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
             Response.AddHeader("Cache-control", "no-store, must-revalidate,private,no-cache");
            Response.AddHeader("Pragma", "no-cache");
            Response.AddHeader("Expires", "0");

            Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);

              if(Session["EmployeeId"] == null)
            {
                Response.Write("Please Login!");
                Response.End();
            }
           

           
            if (!IsPostBack)
            {
                IBusinessProject businesProject = new BusinessProject();
                IList<IProject> projects = businesProject.GetProjects();
                ddlProject.DataSource = projects;
                ddlProject.DataTextField = "ProjectDescription";
                ddlProject.DataValueField = "ProjectId";
                ddlProject.DataBind();
                ddlProject.Items.Insert(0, "Select");

            }
            else
            {
                if (Request.Form["__EVENTTARGET"] == "refresh")
                {
                    BindGridView();
                }
            }

        }
        protected void FillRelease(string projectId)
        {
            IBusinessRelease businessRelease = new BusinessRelease();
            IList<IRelease> releases = businessRelease.GetReleases(int.Parse(projectId.ToString()));
            ddlRelease.DataSource = releases;
            ddlRelease.DataTextField = "Description";
            ddlRelease.DataValueField = "ReleaseId";
            ddlRelease.DataBind();
            ddlRelease.Items.Insert(0, "Select");
        }

        protected void ddlProject_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            FillRelease(ddlProject.SelectedValue);
        }
        protected void FillTask(string projectId, string releaseId)
        {
            IBusinessTask businessTask = new BusinessTask();
            IList<ITask> tasks = businessTask.GetTasks(int.Parse(projectId.ToString()), int.Parse(releaseId.ToString()));
            ddlTask.DataSource = tasks;
            ddlTask.DataTextField = "TaskShortName";
            ddlTask.DataValueField = "TaskId";
            ddlTask.DataBind();
            ddlTask.Items.Insert(0, "Select");

        }

        protected void ddlRelease_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            FillTask(ddlProject.SelectedValue, ddlRelease.SelectedValue);
        }
        protected string getSprint(object sprintId)
        {
            IBusinessSprint businessSprint = new BusinessSprint();
            ISprint sprint = businessSprint.GetSprint(int.Parse(sprintId.ToString()));
            return sprint.SprintDescription;
        }

        protected void BindGridView()
        {
            int taskId = (int.Parse(ddlTask.SelectedValue));
            IBusinessUserStory businessUserStory = new BusinessUserStory();
            IList<IUserStory> userStories = businessUserStory.GetUserStories(taskId);
            ViewState["DataSource"] = userStories;
            gvResults.DataSource = userStories;
            gvResults.DataBind();
        }
        protected void btnGetUserStory_Click(object sender, EventArgs e)
        {
            BindGridView();
        }

        protected void gvResults_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            string userStoryId;
           switch( e.CommandName)
           {
               case "EditUserStory":

                   userStoryId = e.CommandArgument.ToString();
                   ClientScript.RegisterStartupScript(this.GetType(), "UpdateAddUserStory", "UpdateAddUserStory(' " + userStoryId + " ');", true);
                   break;
                        
               

           }

        }

        protected void btnDeleteUserStory_Click(object sender, EventArgs e)
        {
            try
            {

                for (int i = 0; i < gvResults.Rows.Count; i++)
                {
                    CheckBox cb = (CheckBox)gvResults.Rows[i].FindControl("chkShowUserStory");
                    Label lbl = (Label)gvResults.Rows[i].FindControl("lblUserStoryId");

                    if (cb.Checked)
                    {
                        string UserStoryId = string.Empty;
                        if (lbl != null)
                        {
                            UserStoryId = lbl.Text;
                            IBusinessUserStory businessUserStory = new BusinessUserStory();
                            businessUserStory.DeleteUserStory(int.Parse(UserStoryId));
                        }
                    }
                }
                
                lblMsg.Text = "Deleted";
                BindGridView();
            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.Message;
            }
        }

    }
}


          